package com.bms.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

import com.bms.dao.AppFileDao;
import com.bms.entity.AppFile;
import com.bms.service.AppFileService;

/**
 * Service实现类 - 站点文件
 */
@Service
public class AppFileServiceImpl extends BaseServiceImpl<AppFile, String> implements AppFileService {

	@Resource
	private AppFileDao appFileDao;

	@Resource
	public void setBaseDao(AppFileDao appFileDao) {
		super.setBaseDao(appFileDao);
	}

	/**
	 * @return the appFileDao
	 */
	public AppFileDao getAppFileDao() {
		return appFileDao;
	}

	/**
	 * @param appFileDao the appFileDao to set
	 */
	public void setAppFileDao(AppFileDao appFileDao) {
		this.appFileDao = appFileDao;
	}

	public List<AppFile> getFileList(AppFile entity) {
		List<String> propertyNames = new ArrayList<String>();
		if(entity != null){
			if(StringUtils.isNotBlank(entity.getAppEngName()))
				propertyNames.add("appEngName");
			if(StringUtils.isNotBlank(entity.getFilePath()))
				propertyNames.add("filePath");
			if(entity.getNeedDel() != null)
				propertyNames.add("needDel");
			if(entity.getFileStatus() != null)
				propertyNames.add("fileStatus");
		}
		int size = propertyNames.size();
		return appFileDao.getList(entity, propertyNames.toArray(new String[size]));
	}
	
	public List<AppFile> getLike(AppFile entity) {
		List<String> propertyNames = new ArrayList<String>();
		if(entity != null){
			if(StringUtils.isNotBlank(entity.getAppEngName()))
				propertyNames.add("appEngName");
			if(StringUtils.isNotBlank(entity.getFilePath()))
				propertyNames.add("filePath");
			if(entity.getNeedDel() != null)
				propertyNames.add("needDel");
			if(entity.getFileStatus() != null)
				propertyNames.add("fileStatus");
		}
		int size = propertyNames.size();
		return appFileDao.getLike(entity, propertyNames.toArray(new String[size]));
	}

	public boolean hasSenstWords(String appName) {
		return appFileDao.hasSenstWords(appName);
	}

	public List<AppFile> getHigherAuditFiles(String appEnglishName, Integer compareLevel) {
		DetachedCriteria criteria = DetachedCriteria.forClass(AppFile.class);
		criteria.add(Restrictions.eq("appEngName", appEnglishName));
		criteria.add(Restrictions.gt("fileStatus", compareLevel));
		return appFileDao.getList(criteria);
	}

	public List<Integer> getAllAuditLevel(String appEnglishName) {
		return appFileDao.getAllAuditLevel(appEnglishName);
	}

	public List<AppFile> getLowerAuditFiles(Integer compareLevel, String appEnglishName) {
		DetachedCriteria criteria = DetachedCriteria.forClass(AppFile.class);
		criteria.add(Restrictions.lt("fileStatus", compareLevel));
		criteria.add(Restrictions.eq("appEngName", appEnglishName));
		return appFileDao.getList(criteria);
	}
}